// stores/theme.ts
import { writable, derived } from 'svelte/store';

export type ThemeMode = 'light' | 'dark';
export type ColorType = 'primary' | 'secondary' | 'success' | 'warning' | 'error' | 'info';
export const COLOR_NAME_MAP: Record<ColorType, string> = {
	primary: '主色',
	secondary: '辅色',
	success: '成功色',
	warning: '警告色',
	error: '错误色',
	info: '信息色'
};
export type PaletteName =
	| 'cyan'
	| 'sky'
	| 'blue'
	| 'indigo'
	| 'violet'
	| 'purple'
	| 'fuchsia'
	| 'lime'
	| 'green'
	| 'emerald'
	| 'teal'
	| 'orange'
	| 'amber'
	| 'yellow'
	| 'red'
	| 'pink'
	| 'rose'
	| 'stone'
	| 'gray'
	| 'zinc'
	| 'neutral';
type ColorShade =
	| '50'
	| '100'
	| '200'
	| '300'
	| '400'
	| '500'
	| '600'
	| '700'
	| '800'
	| '900'
	| '950';
// 基础色系类型定义
interface Palette {
	light: Record<ColorShade, string>;
	dark: Record<ColorShade, string>;
}
//定义布局模式
type LayoutMode = 'left' | 'top' | 'mixed';
// 定义colorMappings的类型
type ColorMappings = Record<ColorType, PaletteName>;
// 定义配置参数的类型
interface SyncThemeVariablesConfig {
	currentTheme: ThemeMode;
	currentLayout: LayoutMode;
	colorMappings: ColorMappings;
}

// 定义所有基础色系的色值（从用户提供的CSS中提取）
export const basePalettes: Record<PaletteName, Palette> = {
	// 主色系选项
	red: {
		light: {
			'50': 'oklch(0.971 0.013 17.38)',
			'100': 'oklch(0.936 0.032 17.717)',
			'200': 'oklch(0.885 0.062 18.334)',
			'300': 'oklch(0.808 0.114 19.571)',
			'400': 'oklch(0.704 0.191 22.216)',
			'500': 'oklch(0.637 0.237 25.331)',
			'600': 'oklch(0.577 0.245 27.325)',
			'700': 'oklch(0.505 0.213 27.518)',
			'800': 'oklch(0.444 0.177 26.899)',
			'900': 'oklch(0.396 0.141 25.723)',
			'950': 'oklch(0.258 0.092 26.042)'
		},
		dark: {
			'50': 'oklch(0.258 0.092 26.042)',
			'100': 'oklch(0.396 0.141 25.723)',
			'200': 'oklch(0.444 0.177 26.899)',
			'300': 'oklch(0.505 0.213 27.518)',
			'400': 'oklch(0.577 0.245 27.325)',
			'500': 'oklch(0.637 0.237 25.331)',
			'600': 'oklch(0.704 0.191 22.216)',
			'700': 'oklch(0.808 0.114 19.571)',
			'800': 'oklch(0.885 0.062 18.334)',
			'900': 'oklch(0.936 0.032 17.717)',
			'950': 'oklch(0.971 0.013 17.38)'
		}
	},
	orange: {
		light: {
			'50': 'oklch(0.98 0.016 73.684)',
			'100': 'oklch(0.954 0.038 75.164)',
			'200': 'oklch(0.901 0.076 70.697)',
			'300': 'oklch(0.837 0.128 66.29)',
			'400': 'oklch(0.75 0.183 55.934)',
			'500': 'oklch(0.705 0.213 47.604)',
			'600': 'oklch(0.646 0.222 41.116)',
			'700': 'oklch(0.553 0.195 38.402)',
			'800': 'oklch(0.47 0.157 37.304)',
			'900': 'oklch(0.408 0.123 38.172)',
			'950': 'oklch(0.266 0.079 36.259)'
		},
		dark: {
			'50': 'oklch(0.266 0.079 36.259)',
			'100': 'oklch(0.408 0.123 38.172)',
			'200': 'oklch(0.47 0.157 37.304)',
			'300': 'oklch(0.553 0.195 38.402)',
			'400': 'oklch(0.646 0.222 41.116)',
			'500': 'oklch(0.705 0.213 47.604)',
			'600': 'oklch(0.75 0.183 55.934)',
			'700': 'oklch(0.837 0.128 66.29)',
			'800': 'oklch(0.901 0.076 70.697)',
			'900': 'oklch(0.954 0.038 75.164)',
			'950': 'oklch(0.98 0.016 73.684)'
		}
	},
	amber: {
		light: {
			'50': 'oklch(0.987 0.022 95.277)',
			'100': 'oklch(0.962 0.059 95.617)',
			'200': 'oklch(0.924 0.12 95.746)',
			'300': 'oklch(0.879 0.169 91.605)',
			'400': 'oklch(0.828 0.189 84.429)',
			'500': 'oklch(0.769 0.188 70.08)',
			'600': 'oklch(0.666 0.179 58.318)',
			'700': 'oklch(0.555 0.163 48.998)',
			'800': 'oklch(0.473 0.137 46.201)',
			'900': 'oklch(0.414 0.112 45.904)',
			'950': 'oklch(0.279 0.077 45.635)'
		},
		dark: {
			'50': 'oklch(0.279 0.077 45.635)',
			'100': 'oklch(0.414 0.112 45.904)',
			'200': 'oklch(0.473 0.137 46.201)',
			'300': 'oklch(0.555 0.163 48.998)',
			'400': 'oklch(0.666 0.179 58.318)',
			'500': 'oklch(0.769 0.188 70.08)',
			'600': 'oklch(0.828 0.189 84.429)',
			'700': 'oklch(0.879 0.169 91.605)',
			'800': 'oklch(0.924 0.12 95.746)',
			'900': 'oklch(0.962 0.059 95.617)',
			'950': 'oklch(0.987 0.022 95.277)'
		}
	},
	yellow: {
		light: {
			'50': 'oklch(0.987 0.026 102.212)',
			'100': 'oklch(0.973 0.071 103.193)',
			'200': 'oklch(0.945 0.129 101.54)',
			'300': 'oklch(0.905 0.182 98.111)',
			'400': 'oklch(0.852 0.199 91.936)',
			'500': 'oklch(0.795 0.184 86.047)',
			'600': 'oklch(0.681 0.162 75.834)',
			'700': 'oklch(0.554 0.135 66.442)',
			'800': 'oklch(0.476 0.114 61.907)',
			'900': 'oklch(0.421 0.095 57.708)',
			'950': 'oklch(0.286 0.066 53.813)'
		},
		dark: {
			'50': 'oklch(0.286 0.066 53.813)',
			'100': 'oklch(0.421 0.095 57.708)',
			'200': 'oklch(0.476 0.114 61.907)',
			'300': 'oklch(0.554 0.135 66.442)',
			'400': 'oklch(0.681 0.162 75.834)',
			'500': 'oklch(0.795 0.184 86.047)',
			'600': 'oklch(0.852 0.199 91.936)',
			'700': 'oklch(0.905 0.182 98.111)',
			'800': 'oklch(0.945 0.129 101.54)',
			'900': 'oklch(0.973 0.071 103.193)',
			'950': 'oklch(0.987 0.026 102.212)'
		}
	},
	lime: {
		light: {
			'50': 'oklch(0.986 0.031 120.757)',
			'100': 'oklch(0.967 0.067 122.328)',
			'200': 'oklch(0.938 0.127 124.321)',
			'300': 'oklch(0.897 0.196 126.665)',
			'400': 'oklch(0.841 0.238 128.85)',
			'500': 'oklch(0.768 0.233 130.85)',
			'600': 'oklch(0.648 0.2 131.684)',
			'700': 'oklch(0.532 0.157 131.589)',
			'800': 'oklch(0.453 0.124 130.933)',
			'900': 'oklch(0.405 0.101 131.063)',
			'950': 'oklch(0.274 0.072 132.109)'
		},
		dark: {
			'50': 'oklch(0.274 0.072 132.109)',
			'100': 'oklch(0.405 0.101 131.063)',
			'200': 'oklch(0.453 0.124 130.933)',
			'300': 'oklch(0.532 0.157 131.589)',
			'400': 'oklch(0.648 0.2 131.684)',
			'500': 'oklch(0.768 0.233 130.85)',
			'600': 'oklch(0.841 0.238 128.85)',
			'700': 'oklch(0.897 0.196 126.665)',
			'800': 'oklch(0.938 0.127 124.321)',
			'900': 'oklch(0.967 0.067 122.328)',
			'950': 'oklch(0.986 0.031 120.757)'
		}
	},
	green: {
		light: {
			'50': 'oklch(0.982 0.018 155.826)',
			'100': 'oklch(0.962 0.044 156.743)',
			'200': 'oklch(0.925 0.084 155.995)',
			'300': 'oklch(0.871 0.15 154.449)',
			'400': 'oklch(0.792 0.209 151.711)',
			'500': 'oklch(0.723 0.219 149.579)',
			'600': 'oklch(0.627 0.194 149.214)',
			'700': 'oklch(0.527 0.154 150.069)',
			'800': 'oklch(0.448 0.119 151.328)',
			'900': 'oklch(0.393 0.095 152.535)',
			'950': 'oklch(0.266 0.065 152.934)'
		},
		dark: {
			'50': 'oklch(0.266 0.065 152.934)',
			'100': 'oklch(0.393 0.095 152.535)',
			'200': 'oklch(0.448 0.119 151.328)',
			'300': 'oklch(0.527 0.154 150.069)',
			'400': 'oklch(0.627 0.194 149.214)',
			'500': 'oklch(0.723 0.219 149.579)',
			'600': 'oklch(0.792 0.209 151.711)',
			'700': 'oklch(0.871 0.15 154.449)',
			'800': 'oklch(0.925 0.084 155.995)',
			'900': 'oklch(0.962 0.044 156.743)',
			'950': 'oklch(0.982 0.018 155.826)'
		}
	},
	emerald: {
		light: {
			'50': 'oklch(0.979 0.021 166.113)',
			'100': 'oklch(0.95 0.052 163.051)',
			'200': 'oklch(0.905 0.093 164.15)',
			'300': 'oklch(0.845 0.143 164.978)',
			'400': 'oklch(0.765 0.177 163.223)',
			'500': 'oklch(0.696 0.17 162.48)',
			'600': 'oklch(0.596 0.145 163.225)',
			'700': 'oklch(0.508 0.118 165.612)',
			'800': 'oklch(0.432 0.095 166.913)',
			'900': 'oklch(0.378 0.077 168.94)',
			'950': 'oklch(0.262 0.051 172.552)'
		},
		dark: {
			'50': 'oklch(0.262 0.051 172.552)',
			'100': 'oklch(0.378 0.077 168.94)',
			'200': 'oklch(0.432 0.095 166.913)',
			'300': 'oklch(0.508 0.118 165.612)',
			'400': 'oklch(0.596 0.145 163.225)',
			'500': 'oklch(0.696 0.17 162.48)',
			'600': 'oklch(0.765 0.177 163.223)',
			'700': 'oklch(0.845 0.143 164.978)',
			'800': 'oklch(0.905 0.093 164.15)',
			'900': 'oklch(0.95 0.052 163.051)',
			'950': 'oklch(0.979 0.021 166.113)'
		}
	},
	teal: {
		light: {
			'50': 'oklch(0.984 0.014 180.72)',
			'100': 'oklch(0.953 0.051 180.801)',
			'200': 'oklch(0.91 0.096 180.426)',
			'300': 'oklch(0.855 0.138 181.071)',
			'400': 'oklch(0.777 0.152 181.912)',
			'500': 'oklch(0.704 0.14 182.503)',
			'600': 'oklch(0.6 0.118 184.704)',
			'700': 'oklch(0.511 0.096 186.391)',
			'800': 'oklch(0.437 0.078 188.216)',
			'900': 'oklch(0.386 0.063 188.416)',
			'950': 'oklch(0.277 0.046 192.524)'
		},
		dark: {
			'50': 'oklch(0.277 0.046 192.524)',
			'100': 'oklch(0.386 0.063 188.416)',
			'200': 'oklch(0.437 0.078 188.216)',
			'300': 'oklch(0.511 0.096 186.391)',
			'400': 'oklch(0.6 0.118 184.704)',
			'500': 'oklch(0.704 0.14 182.503)',
			'600': 'oklch(0.777 0.152 181.912)',
			'700': 'oklch(0.855 0.138 181.071)',
			'800': 'oklch(0.91 0.096 180.426)',
			'900': 'oklch(0.953 0.051 180.801)',
			'950': 'oklch(0.984 0.014 180.72)'
		}
	},
	cyan: {
		light: {
			'50': 'oklch(0.984 0.019 200.873)',
			'100': 'oklch(0.956 0.045 203.388)',
			'200': 'oklch(0.917 0.08 205.041)',
			'300': 'oklch(0.865 0.127 207.078)',
			'400': 'oklch(0.789 0.154 211.53)',
			'500': 'oklch(0.715 0.143 215.221)',
			'600': 'oklch(0.609 0.126 221.723)',
			'700': 'oklch(0.52 0.105 223.128)',
			'800': 'oklch(0.45 0.085 224.283)',
			'900': 'oklch(0.398 0.07 227.392)',
			'950': 'oklch(0.302 0.056 229.695)'
		},
		dark: {
			'50': 'oklch(0.302 0.056 229.695)',
			'100': 'oklch(0.398 0.07 227.392)',
			'200': 'oklch(0.45 0.085 224.283)',
			'300': 'oklch(0.52 0.105 223.128)',
			'400': 'oklch(0.609 0.126 221.723)',
			'500': 'oklch(0.715 0.143 215.221)',
			'600': 'oklch(0.789 0.154 211.53)',
			'700': 'oklch(0.865 0.127 207.078)',
			'800': 'oklch(0.917 0.08 205.041)',
			'900': 'oklch(0.956 0.045 203.388)',
			'950': 'oklch(0.984 0.019 200.873)'
		}
	},
	sky: {
		light: {
			'50': 'oklch(0.977 0.013 236.62)',
			'100': 'oklch(0.951 0.026 236.824)',
			'200': 'oklch(0.901 0.058 230.902)',
			'300': 'oklch(0.828 0.111 230.318)',
			'400': 'oklch(0.746 0.16 232.661)',
			'500': 'oklch(0.685 0.169 237.323)',
			'600': 'oklch(0.588 0.158 241.966)',
			'700': 'oklch(0.5 0.134 242.749)',
			'800': 'oklch(0.443 0.11 240.79)',
			'900': 'oklch(0.391 0.09 240.876)',
			'950': 'oklch(0.293 0.066 243.157)'
		},
		dark: {
			'50': 'oklch(0.293 0.066 243.157)',
			'100': 'oklch(0.391 0.09 240.876)',
			'200': 'oklch(0.443 0.11 240.79)',
			'300': 'oklch(0.5 0.134 242.749)',
			'400': 'oklch(0.588 0.158 241.966)',
			'500': 'oklch(0.685 0.169 237.323)',
			'600': 'oklch(0.746 0.16 232.661)',
			'700': 'oklch(0.828 0.111 230.318)',
			'800': 'oklch(0.901 0.058 230.902)',
			'900': 'oklch(0.951 0.026 236.824)',
			'950': 'oklch(0.977 0.013 236.62)'
		}
	},
	blue: {
		light: {
			'50': 'oklch(0.97 0.014 254.604)',
			'100': 'oklch(0.932 0.032 255.585)',
			'200': 'oklch(0.882 0.059 254.128)',
			'300': 'oklch(0.809 0.105 251.813)',
			'400': 'oklch(0.707 0.165 254.624)',
			'500': 'oklch(0.623 0.214 259.815)',
			'600': 'oklch(0.546 0.245 262.881)',
			'700': 'oklch(0.488 0.243 264.376)',
			'800': 'oklch(0.424 0.199 265.638)',
			'900': 'oklch(0.379 0.146 265.522)',
			'950': 'oklch(0.282 0.091 267.935)'
		},
		dark: {
			'50': 'oklch(0.282 0.091 267.935)',
			'100': 'oklch(0.379 0.146 265.522)',
			'200': 'oklch(0.424 0.199 265.638)',
			'300': 'oklch(0.488 0.243 264.376)',
			'400': 'oklch(0.546 0.245 262.881)',
			'500': 'oklch(0.623 0.214 259.815)',
			'600': 'oklch(0.707 0.165 254.624)',
			'700': 'oklch(0.809 0.105 251.813)',
			'800': 'oklch(0.882 0.059 254.128)',
			'900': 'oklch(0.932 0.032 255.585)',
			'950': 'oklch(0.97 0.014 254.604)'
		}
	},
	indigo: {
		light: {
			'50': 'oklch(0.962 0.018 272.314)',
			'100': 'oklch(0.93 0.034 272.788)',
			'200': 'oklch(0.87 0.065 274.039)',
			'300': 'oklch(0.785 0.115 274.713)',
			'400': 'oklch(0.673 0.182 276.935)',
			'500': 'oklch(0.585 0.233 277.117)',
			'600': 'oklch(0.511 0.262 276.966)',
			'700': 'oklch(0.457 0.24 277.023)',
			'800': 'oklch(0.398 0.195 277.366)',
			'900': 'oklch(0.359 0.144 278.697)',
			'950': 'oklch(0.257 0.09 281.288)'
		},
		dark: {
			'50': 'oklch(0.257 0.09 281.288)',
			'100': 'oklch(0.359 0.144 278.697)',
			'200': 'oklch(0.398 0.195 277.366)',
			'300': 'oklch(0.457 0.24 277.023)',
			'400': 'oklch(0.511 0.262 276.966)',
			'500': 'oklch(0.585 0.233 277.117)',
			'600': 'oklch(0.673 0.182 276.935)',
			'700': 'oklch(0.785 0.115 274.713)',
			'800': 'oklch(0.87 0.065 274.039)',
			'900': 'oklch(0.93 0.034 272.788)',
			'950': 'oklch(0.962 0.018 272.314)'
		}
	},
	violet: {
		light: {
			'50': 'oklch(0.969 0.016 293.756)',
			'100': 'oklch(0.943 0.029 294.588)',
			'200': 'oklch(0.894 0.057 293.283)',
			'300': 'oklch(0.811 0.111 293.571)',
			'400': 'oklch(0.702 0.183 293.541)',
			'500': 'oklch(0.606 0.25 292.717)',
			'600': 'oklch(0.541 0.281 293.009)',
			'700': 'oklch(0.491 0.27 292.581)',
			'800': 'oklch(0.432 0.232 292.759)',
			'900': 'oklch(0.38 0.189 293.745)',
			'950': 'oklch(0.283 0.141 291.089)'
		},
		dark: {
			'50': 'oklch(0.283 0.141 291.089)',
			'100': 'oklch(0.38 0.189 293.745)',
			'200': 'oklch(0.432 0.232 292.759)',
			'300': 'oklch(0.491 0.27 292.581)',
			'400': 'oklch(0.541 0.281 293.009)',
			'500': 'oklch(0.606 0.25 292.717)',
			'600': 'oklch(0.702 0.183 293.541)',
			'700': 'oklch(0.811 0.111 293.571)',
			'800': 'oklch(0.894 0.057 293.283)',
			'900': 'oklch(0.943 0.029 294.588)',
			'950': 'oklch(0.969 0.016 293.756)'
		}
	},
	purple: {
		light: {
			'50': 'oklch(0.977 0.014 308.299)',
			'100': 'oklch(0.946 0.033 307.174)',
			'200': 'oklch(0.902 0.063 306.703)',
			'300': 'oklch(0.827 0.119 306.383)',
			'400': 'oklch(0.714 0.203 305.504)',
			'500': 'oklch(0.627 0.265 303.9)',
			'600': 'oklch(0.558 0.288 302.321)',
			'700': 'oklch(0.496 0.265 301.924)',
			'800': 'oklch(0.438 0.218 303.724)',
			'900': 'oklch(0.381 0.176 304.987)',
			'950': 'oklch(0.291 0.149 302.717)'
		},
		dark: {
			'50': 'oklch(0.291 0.149 302.717)',
			'100': 'oklch(0.381 0.176 304.987)',
			'200': 'oklch(0.438 0.218 303.724)',
			'300': 'oklch(0.496 0.265 301.924)',
			'400': 'oklch(0.558 0.288 302.321)',
			'500': 'oklch(0.627 0.265 303.9)',
			'600': 'oklch(0.714 0.203 305.504)',
			'700': 'oklch(0.827 0.119 306.383)',
			'800': 'oklch(0.902 0.063 306.703)',
			'900': 'oklch(0.946 0.033 307.174)',
			'950': 'oklch(0.977 0.014 308.299)'
		}
	},
	fuchsia: {
		light: {
			'50': 'oklch(0.977 0.017 320.058)',
			'100': 'oklch(0.952 0.037 318.852)',
			'200': 'oklch(0.903 0.076 319.62)',
			'300': 'oklch(0.833 0.145 321.434)',
			'400': 'oklch(0.74 0.238 322.16)',
			'500': 'oklch(0.667 0.295 322.15)',
			'600': 'oklch(0.591 0.293 322.896)',
			'700': 'oklch(0.518 0.253 323.949)',
			'800': 'oklch(0.452 0.211 324.591)',
			'900': 'oklch(0.401 0.17 325.612)',
			'950': 'oklch(0.293 0.136 325.661)'
		},
		dark: {
			'50': 'oklch(0.293 0.136 325.661)',
			'100': 'oklch(0.401 0.17 325.612)',
			'200': 'oklch(0.452 0.211 324.591)',
			'300': 'oklch(0.518 0.253 323.949)',
			'400': 'oklch(0.591 0.293 322.896)',
			'500': 'oklch(0.667 0.295 322.15)',
			'600': 'oklch(0.74 0.238 322.16)',
			'700': 'oklch(0.833 0.145 321.434)',
			'800': 'oklch(0.903 0.076 319.62)',
			'900': 'oklch(0.952 0.037 318.852)',
			'950': 'oklch(0.977 0.017 320.058)'
		}
	},
	pink: {
		light: {
			'50': 'oklch(0.971 0.014 343.198)',
			'100': 'oklch(0.948 0.028 342.258)',
			'200': 'oklch(0.899 0.061 343.231)',
			'300': 'oklch(0.823 0.12 346.018)',
			'400': 'oklch(0.718 0.202 349.761)',
			'500': 'oklch(0.656 0.241 354.308)',
			'600': 'oklch(0.592 0.249 0.584)',
			'700': 'oklch(0.525 0.223 3.958)',
			'800': 'oklch(0.459 0.187 3.815)',
			'900': 'oklch(0.408 0.153 2.432)',
			'950': 'oklch(0.284 0.109 3.907)'
		},
		dark: {
			'50': 'oklch(0.284 0.109 3.907)',
			'100': 'oklch(0.408 0.153 2.432)',
			'200': 'oklch(0.459 0.187 3.815)',
			'300': 'oklch(0.525 0.223 3.958)',
			'400': 'oklch(0.592 0.249 0.584)',
			'500': 'oklch(0.656 0.241 354.308)',
			'600': 'oklch(0.718 0.202 349.761)',
			'700': 'oklch(0.823 0.12 346.018)',
			'800': 'oklch(0.899 0.061 343.231)',
			'900': 'oklch(0.948 0.028 342.258)',
			'950': 'oklch(0.971 0.014 343.198)'
		}
	},
	rose: {
		light: {
			'50': 'oklch(0.969 0.015 12.422)',
			'100': 'oklch(0.941 0.03 12.58)',
			'200': 'oklch(0.892 0.058 10.001)',
			'300': 'oklch(0.81 0.117 11.638)',
			'400': 'oklch(0.712 0.194 13.428)',
			'500': 'oklch(0.645 0.246 16.439)',
			'600': 'oklch(0.586 0.253 17.585)',
			'700': 'oklch(0.514 0.222 16.935)',
			'800': 'oklch(0.455 0.188 13.697)',
			'900': 'oklch(0.41 0.159 10.272)',
			'950': 'oklch(0.271 0.105 12.094)'
		},
		dark: {
			'50': 'oklch(0.271 0.105 12.094)',
			'100': 'oklch(0.41 0.159 10.272)',
			'200': 'oklch(0.455 0.188 13.697)',
			'300': 'oklch(0.514 0.222 16.935)',
			'400': 'oklch(0.586 0.253 17.585)',
			'500': 'oklch(0.645 0.246 16.439)',
			'600': 'oklch(0.712 0.194 13.428)',
			'700': 'oklch(0.81 0.117 11.638)',
			'800': 'oklch(0.892 0.058 10.001)',
			'900': 'oklch(0.941 0.03 12.58)',
			'950': 'oklch(0.969 0.015 12.422)'
		}
	},
	stone: {
		light: {
			'50': 'oklch(0.985 0.001 106.423)',
			'100': 'oklch(0.97 0.001 106.424)',
			'200': 'oklch(0.923 0.003 48.717)',
			'300': 'oklch(0.869 0.005 56.366)',
			'400': 'oklch(0.709 0.01 56.259)',
			'500': 'oklch(0.553 0.013 58.071)',
			'600': 'oklch(0.444 0.011 73.639)',
			'700': 'oklch(0.374 0.01 67.558)',
			'800': 'oklch(0.268 0.007 34.298)',
			'900': 'oklch(0.216 0.006 56.043)',
			'950': 'oklch(0.147 0.004 49.25)'
		},
		dark: {			
			'50': 'oklch(0.216 0.006 56.043)',
			'100': 'oklch(0.268 0.007 34.298)',
			'200': 'oklch(0.374 0.01 67.558)',
			'300': 'oklch(0.444 0.011 73.639)',
			'400': 'oklch(0.553 0.013 58.071)',
			'500': 'oklch(0.709 0.01 56.259)',
			'600': 'oklch(0.869 0.005 56.366)',
			'700': 'oklch(0.923 0.003 48.717)',
			'800': 'oklch(0.97 0.001 106.424)',
			'900': 'oklch(0.985 0.001 106.423)',
			'950': 'oklch(1 0 0)',
		}
	},
	gray: {
		light: {
			'50': 'oklch(0.985 0.002 247.839)',
			'100': 'oklch(0.967 0.003 264.542)',
			'200': 'oklch(0.928 0.006 264.531)',
			'300': 'oklch(0.872 0.01 258.338)',
			'400': 'oklch(0.707 0.022 261.325)',
			'500': 'oklch(0.551 0.027 264.364)',
			'600': 'oklch(0.446 0.03 256.802)',
			'700': 'oklch(0.373 0.034 259.733)',
			'800': 'oklch(0.278 0.033 256.848)',
			'900': 'oklch(0.21 0.034 264.665)',
			'950': 'oklch(0.13 0.028 261.692)'
		},
		dark: {			
			'50': 'oklch(0.21 0.034 264.665)',
			'100': 'oklch(0.278 0.033 256.848)',
			'200': 'oklch(0.373 0.034 259.733)',
			'300': 'oklch(0.446 0.03 256.802)',
			'400': 'oklch(0.551 0.027 264.364)',
			'500': 'oklch(0.707 0.022 261.325)',
			'600': 'oklch(0.872 0.01 258.338)',
			'700': 'oklch(0.928 0.006 264.531)',
			'800': 'oklch(0.967 0.003 264.542)',
			'900': 'oklch(0.985 0.002 247.839)',
			'950': 'oklch(1 0 0)',
		}
	},
	zinc: {
		light: {
			'50': 'oklch(0.985 0 0)',
			'100': 'oklch(0.967 0.001 286.375)',
			'200': 'oklch(0.92 0.004 286.32)',
			'300': 'oklch(0.871 0.006 286.286)',
			'400': 'oklch(0.705 0.015 286.067)',
			'500': 'oklch(0.552 0.016 285.938)',
			'600': 'oklch(0.442 0.017 285.786)',
			'700': 'oklch(0.37 0.013 285.805)',
			'800': 'oklch(0.274 0.006 286.033)',
			'900': 'oklch(0.21 0.006 285.885)',
			'950': 'oklch(0.141 0.005 285.823)'
		},
		dark: {
			'50': 'oklch(0.21 0.006 285.885)',
			'100': 'oklch(0.274 0.006 286.033)',
			'200': 'oklch(0.37 0.013 285.805)',
			'300': 'oklch(0.442 0.017 285.786)',
			'400': 'oklch(0.552 0.016 285.938)',
			'500': 'oklch(0.705 0.015 286.067)',
			'600': 'oklch(0.871 0.006 286.286)',
			'700': 'oklch(0.92 0.004 286.32)',
			'800': 'oklch(0.967 0.001 286.375)',
			'900': 'oklch(0.985 0 0)',
			'950': 'oklch(1 0 0)',
		}
	},
	neutral: {
		light: {
			'50': 'oklch(0.985 0 0)',
			'100': 'oklch(0.97 0 0)',
			'200': 'oklch(0.922 0 0)',
			'300': 'oklch(0.87 0 0)',
			'400': 'oklch(0.715 0 0)',
			'500': 'oklch(0.556 0 0)',
			'600': 'oklch(0.439 0 0)',
			'700': 'oklch(0.371 0 0)',
			'800': 'oklch(0.269 0 0)',
			'900': 'oklch(0.205 0 0)',
			'950': 'oklch(0.145 0 0)'
		},
		dark: {
			'50': 'oklch(0.205 0 0)',
			'100': 'oklch(0.269 0 0)',
			'200': 'oklch(0.371 0 0)',
			'300': 'oklch(0.439 0 0)',
			'400': 'oklch(0.556 0 0)',
			'500': 'oklch(0.715 0 0)',
			'600': 'oklch(0.87 0 0)',
			'700': 'oklch(0.922 0 0)',
			'800': 'oklch(0.97 0 0)',
			'900': 'oklch(0.985 0 0)',
			'950': 'oklch(1 0 0)',
		}
	}
};

// 定义其他色值
const extraPalettes: {
	light: Record<string, string>;
	dark: Record<string, string>;
} = {
	light: {
		'--color-background': 'var(--color-info-200)',
		'--color-page': 'oklch(1 0 0)',
		'--color-border': 'var(--color-info-200)',
		'--color-overlay': 'oklch(0 0 0 / 0.3)',
		'--color-shadow': 'oklch(0 0 0 / 0.4)',
		'--color-fill': 'color-mix(in srgb, var(--color-info-700) 10%, transparent)',
		'--color-fill-light': 'oklch(1 0 0)',
		'--color-disabled': 'oklch(1 0 0 / 0.6)',
	},
	dark: {
		'--color-background': 'var(--color-info-50)',
		'--color-page': 'var(--color-info-100)',
		'--color-border': 'var(--color-info-300)',
		'--color-overlay': 'oklch(1 0 0 / 0.3)',
		'--color-shadow': 'oklch(0 0 0 / 0.6)',
		'--color-fill': 'color-mix(in srgb, var(--color-info-700) 10%, transparent)',
		'--color-fill-light': 'var(--color-info-400)',
		'--color-disabled': 'oklch(0 0 0 / 0.6)',
	}
};

// 定义5类主题色及其可选基础色系
export const themeColorConfig: Record<ColorType, PaletteName[]> = {
	primary: ['cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia'],
	secondary: ['cyan', 'sky', 'blue', 'indigo', 'violet', 'purple', 'fuchsia'],
	success: ['lime', 'green', 'emerald', 'teal'],
	warning: ['orange', 'amber', 'yellow'],
	error: ['red', 'pink', 'rose'],
	info: ['stone', 'gray', 'zinc', 'neutral']
};

// 初始配置
const initialConfig: SyncThemeVariablesConfig = {
	currentTheme: 'light', // 默认主题
	currentLayout: 'left',
	colorMappings: {
		primary: 'blue', // 默认primary使用cyan
		secondary: 'violet',
		success: 'emerald', // 默认success使用green
		warning: 'amber', // 默认warning使用amber
		error: 'red', // 默认error使用red
		info: 'zinc' // 默认info使用gray
	}
};

// 创建主题store
export const themeStore = (() => {
	const { subscribe, update } = writable(initialConfig);
	/* -------------------------- 新增：初始化主题样式表 --------------------------*/
	let themeStyleSheet: CSSStyleSheet | null = null;
	// 只在浏览器端创建样式表
	if (typeof document !== 'undefined') {
		// 创建 style 元素
		const styleElement = document.createElement('style');
		styleElement.id = 'theme-variables'; // 标识主题样式表
		document.head.appendChild(styleElement);
		// 获取只读的 sheet 属性（不再赋值，仅读取）
		if (styleElement.sheet) {
			themeStyleSheet = styleElement.sheet as CSSStyleSheet;
		} else {
			console.warn('无法初始化主题样式表，主题切换可能失效');
		}
	}

	// 同步CSS变量到当前主题
	const syncCssVariables = (config: SyncThemeVariablesConfig) => {
		// 只在浏览器环境执行 DOM 操作
		if (typeof document === 'undefined' || !themeStyleSheet) {
			return; // 服务器端直接返回，不执行
		}
		const { currentTheme, colorMappings } = config;
		const targetSelector = `[data-theme="${currentTheme}"]`;
		// 3. 生成主题变量规则：遍历所有 colorType（primary/success 等）
		let cssRules = '';

		// 为每个主题色类型设置CSS变量
		Object.entries(colorMappings).forEach(([colorType, palette]) => {
			// 获取当前主题下的基础色系色值
			const paletteColors = basePalettes[palette]?.[currentTheme];
			const extraColors = extraPalettes[currentTheme];
			if (!paletteColors) return;

			// 为每个色度设置CSS变量
			Object.entries(paletteColors).forEach(([shade, value]) => {
				cssRules += `--color-${colorType}-${shade}: ${value};`;
			});
			if (extraColors) {
				Object.entries(extraColors).forEach(([shade, value]) => {
					cssRules += `${shade}: ${value};`;
				});
			}
		});

		// 清除旧的同主题规则（避免堆积）
		for (let i = themeStyleSheet.cssRules.length - 1; i >= 0; i--) {
			const rule = themeStyleSheet.cssRules[i];
			if (rule instanceof CSSStyleRule && rule.selectorText === targetSelector) {
				themeStyleSheet.deleteRule(i);
			}
		}
		// 5. 添加新的主题规则（变量属于 [data-theme='xxx'] 选择器）
		if (cssRules) {
			themeStyleSheet.insertRule(`${targetSelector} { ${cssRules} }`, 0);
		}

		// 6. 同步根元素的 data-theme 属性（激活当前主题）
		// document.documentElement.setAttribute('data-theme', currentTheme);
	};

	// 初始化时同步一次
	// syncCssVariables(initialConfig);

	return {
		subscribe,
		//初始化用户偏好设置
		iniUserPrefer: ()=>{
			update(() => {
				const newConfig = initialConfig;
				const theme = newConfig.currentTheme;
				// 设置data-theme属性
				document.documentElement.setAttribute('data-theme', theme);
				syncCssVariables(newConfig);

				return newConfig;
			});
		},

		// 切换主题（light/dark）
		switchTheme: (theme: 'light' | 'dark') => {
			update((config) => {
				const newConfig = { ...config, currentTheme: theme };
				// 设置data-theme属性
				document.documentElement.setAttribute('data-theme', theme);
				// 同步CSS变量
				syncCssVariables(newConfig);
				return newConfig;
			});
		},

		// 切换主题色的基础色系
		switchColorPalette: (colorType: keyof typeof themeColorConfig, palette: PaletteName) => {
			update((config) => {
				// 验证是否为有效切换
				if (!themeColorConfig[colorType].includes(palette)) {
					console.warn(`Invalid palette ${palette} for color type ${colorType}`);
					return config;
				}

				const newConfig = {
					...config,
					colorMappings: {
						...config.colorMappings,
						[colorType]: palette
					}
				};

				// 同步CSS变量
				syncCssVariables(newConfig);
				return newConfig;
			});
		},

		//切换布局模式
		switchLayoutMode: (mode: 'left' | 'top' | 'mixed') => {
			update((config) => {
				const newConfig = { ...config, currentLayout: mode };
				return newConfig;
			});
		},

		// 获取当前配置
		getCurrentConfig: () => {
			let config;
			subscribe((value) => (config = value))();
			return config;
		}
	};
})();

// 派生store：获取当前激活的主题
export const currentTheme = derived(themeStore, ($themeStore) => $themeStore.currentTheme);

// 派生store：获取当前激活的布局
export const currentLayout = derived(themeStore, ($themeStore) => $themeStore.currentLayout);

// 派生store：获取当前颜色映射
export const colorMappings = derived(themeStore, ($themeStore) => $themeStore.colorMappings);
